﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using HZ.Supervise.DAL;

namespace HZ.Supervise.Web.Secondphase.Statistics
{
	public partial class StatisticsHospitalByYear : BasePage
	{
		DataTable dt = new DataTable();
		DataTable dtPie = null;
		public string Hid;
        string Hname;
		string year;
        public string sessionid = "";
        string hname = "";

        public StatisticsHospitalByYear()
        {
            this.ModuleNo = Module.报表统计.ToString();
            this.PageOperateType = OperateType.医院月报.ToString();
        }

		protected void Page_Load(object sender, EventArgs e)
		{
            Hid = Request["Y"];
			if (!string.IsNullOrEmpty(Hid))
			{
                hname = HttpUtility.UrlDecode(Request["Y"].ToString());
			}
			if (!IsPostBack)
			{
                //根据Hid获得Name   by张雄
				dataBind();
				BindMSChat();
				GetStatistics();

			}

		}

		/// <summary>
		/// 数据绑定
		/// </summary>
		private void dataBind()
        {
			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

			string timeFrom = string.Intern(this.txtTimeFrom.Text);

			string timeTo = string.Intern(this.txtTimeTo.Text);

			dt = ie.StatisticsHospitalByYear(hname, timeFrom, timeTo);


			HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate", "AddWareHouseRate");//增加新列，主要目的是格式化显示字段

			HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

			DataTable dtGV = new DataTable();

			DataView dv = dt.DefaultView;

			dv.Sort = " daytime desc";

			dtGV = dv.ToTable();

			gvwStatisticsHos.DataSource = dtGV;

			gvwStatisticsHos.DataBind();

			Function.Combine(this.gvwStatisticsHos);

            dtPie = ie.StatisticsHospitalByYearPie(hname, timeFrom, timeTo);

			HZ.Supervise.Web.Function.ChangeRate(dtPie, "WareHouseRate", "AddWareHouseRate");

			HZ.Supervise.Web.Function.ChangeRate(dtPie, "DistributeRate", "AddDistributeRate");

			this.gvwStatisticsHos1.DataSource = dtPie;

			this.gvwStatisticsHos1.DataBind();

			Function.Combine(this.gvwStatisticsHos1);

		}

		/// <summary>
		/// 绑定统计
		/// </summary>
		void GetStatistics()
		{

			double purchaseAmountTotal = 0;//采购总金额

			double distributionAmountTotal = 0;

			double wareHouseAmountTotal = 0;//入库总金额

			for (int i = 0; i < gvwStatisticsHos.Rows.Count; i++)
			{

				purchaseAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[1].Text);

				distributionAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[2].Text);

				wareHouseAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[4].Text);

			}

            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + distributionAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString();

		}

		/// <summary>
		/// 图表曲线绑定
		/// 2011.4.8 by 张雄 create
		/// </summary>
		private void BindMSChat()
		{
			//空为初始页
			if (dt.Rows.Count > 0)
			{
				this.divshow.Visible = true;
				string monch = ""; //月份
				#region 左图

				//***********************基本药品数量******************/
                Series series3 = new Series("采购金额");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series3.MarkerStyle = MarkerStyle.Diamond;
				series3.BorderWidth = 1;
				series3.ShadowOffset = 1;
                series3.Label = "#VAL{F}";
                series3.Color = Color.FromArgb(189, 68, 104);
                series3.ToolTip = "采购金额: #VAL{c} 万元";
				//***************** 2010现价品种*****************/
                Series series4 = new Series("配送金额");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series4.MarkerStyle = MarkerStyle.Diamond;
				series4.BorderWidth = 1;
				series4.ShadowOffset = 1;
				series4.Label = "#VAL{F}";
                series4.Color = Color.FromArgb(100, 107, 253);
                series4.ToolTip = "配送金额: #VAL{c} 万元";
				//***************** 2010现价品种*****************/
                Series series5 = new Series("入库金额");
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series5.MarkerStyle = MarkerStyle.Diamond;
				series5.BorderWidth = 1;
				series5.ShadowOffset = 1;
				series5.Label = "#VAL{F}";
                series5.Color = Color.FromArgb(234, 193, 111);
                series5.ToolTip = "入库金额: #VAL{c} 万元";
				#endregion

				#region 右图

				Series series1 = new Series("药品全年统计");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Pie", true);

				series1.ChartType = SeriesChartType.Pie;

                series1.ToolTip = "#VALX: #VAL{c} 元";

                series1.Label = "#PERCENT";

				series1.ShadowOffset = 2;

				series1.BorderColor = Color.DarkGray;

				#endregion

				#region 饼状图数据统计
				IList<HZ.Supervise.Model.ChartModel> chartList = HZ.Supervise.DAL.DAL_Helper.CommonFillList<HZ.Supervise.Model.ChartModel>(dtPie);


				series1.Points.DataBind(chartList, "SortSource", "PurchaseAmountTotal", "LegendText=SortSource,YValues=PurchaseAmountTotal");
				//曹鹏 2011年5月23日13:19:41 判断采购类型
				for (int i = 0; i < chartList.Count; i++)
				{

					if (chartList[i].SortSource == "日常采购")
					{

						series1.Points[i].Color = Color.FromArgb(74, 172, 197);

					}
					else if (chartList[i].SortSource == "临时采购")
					{

						series1.Points[i].Color = Color.FromArgb(129, 101, 162);

					}
					else if (chartList[i].SortSource == "应急采购")
					{

						series1.Points[i].Color = Color.FromArgb(192, 80, 78);

					}

				}
                #endregion

                #region 柱状图绑定
                int s = 0;

                string str = "";//用来存储时间中月份

                foreach (DataRow row in dt.Rows)
                {
                    monch = HZ.Supervise.Web.Function.FormartDate(row["daytime"].ToString());   //得到月份
                    if (!str.Contains(monch))
                    {
                        str += monch + ","; //把月份加入字符串
                        series3.Points.AddXY(monch, Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000);

                        series4.Points.AddXY(monch, Convert.ToDouble(row["DistributeAmountTotal"].ToString()) / 10000);
                        
                        series5.Points.AddXY(monch, Convert.ToDouble(row["WarehouseAmountTotal"].ToString()) / 10000);
                        
                        s++;
                    }
                }		 
	            #endregion

                #region 绑定到图

				ChartPurchasing.Series.Add(series3);
				ChartPurchasing.Series.Add(series4);

				ChartPurchasing.Series.Add(series5);

                ChartInStorage.Series.Add(series1);

				#endregion

				#region 两张图片属性 11

				ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.IsMarginVisible = true;
				ChartPurchasing.ChartAreas["ChartHosPurchasing"].Area3DStyle.Enable3D = false;


				ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.MajorGrid.Interval = 1;
				ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.LineWidth = 1;
				ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisY.LineWidth = 1;

				ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisY.Title = "金额（万元）";
				ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.Title = "日期";

                ChartInStorage.ChartAreas["ChartHosStorage"].AxisX.IsMarginVisible = false;
                ChartInStorage.ChartAreas["ChartHosStorage"].Area3DStyle.Enable3D = false;


				#endregion

                lblnull.Visible = false;
			}
			else
			{
				//没有数据显示“没数据”
				this.divshow.Visible = false;
				this.ChartPurchasing.Visible = false;
				lblnull.Visible = true;

                this.divTable.Visible = false;
                this.btnExcel1.Visible = false;
			}
		}

		protected void Button1_Click(object sender, EventArgs e)
		{
            if (txtTimeTo.Text.Trim() != "" && txtTimeFrom.Text.Trim() !="")
            {
                if (Convert.ToDateTime(txtTimeFrom.Text.Trim()) > Convert.ToDateTime(txtTimeTo.Text.Trim()))
                {
                      HZ.Web.WebUtil.Alert("开始时间不能大于结束时间",this.Page);
                      this.txtTimeFrom.Text = "";
                      this.txtTimeTo.Text = "";
                }
            }
			dataBind();
			BindMSChat();
			GetStatistics();
		}
		/// <summary>
		///显示模式的转换 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void droYear0_SelectedIndexChanged(object sender, EventArgs e)
		{
			dataBind();
			BindMSChat();
			GetStatistics();
		}

		protected void btnExcel1_Click(object sender, EventArgs e)
		{

			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

			string timeFrom = string.Intern(this.txtTimeFrom.Text);

			string timeTo = string.Intern(this.txtTimeTo.Text);

			dt = ie.StatisticsHospitalByYear(hname, timeFrom, timeTo);

			HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate", "AddWareHouseRate");//增加新列，主要目的是格式化显示字段

			HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

			DataTable dtGV = new DataTable();

			DataView dv = dt.DefaultView;

			dv.Sort = " daytime desc";

			dtGV = dv.ToTable();

			HZ.Excel.ExcelHelper.ExportDataForWeb(dtGV, this.gvwStatisticsHos);

		}

		protected void btnExcel2_Click(Object sender, EventArgs e)
		{

			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

			string timeFrom = string.Intern(this.txtTimeFrom.Text);

			string timeTo = string.Intern(this.txtTimeTo.Text);

			dtPie = ie.StatisticsHospitalByYearPie(hname, timeFrom, timeTo);

			HZ.Supervise.Web.Function.ChangeRate(dtPie, "WareHouseRate", "AddWareHouseRate");

			HZ.Supervise.Web.Function.ChangeRate(dtPie, "DistributeRate", "AddDistributeRate");

			HZ.Excel.ExcelHelper.ExportDataForWeb(dtPie, this.gvwStatisticsHos1);

		}

        /// <summary>
        /// 添加表头
        /// by张雄  2011.5.24
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwStatisticsHos_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Text = (hname == null ? "医院汇总统计" : hname) + "</th></tr><tr>";

                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "日期";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "入库率";

                    break;
            }
        }

        protected void gvwStatisticsHos1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Text = (hname == null ? "医院汇总统计" : hname) + "</th></tr><tr>";

                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "采购类型";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "入库率";

                    break;
            }
        }

	}
}
